package com.hymjweb.demo.hymj.customer_manage.freight_agreement.auto.service;

import com.alibaba.fastjson.JSONObject;
import com.hymjweb.demo.SpringAopUtil;
import com.hymjweb.demo.framework.cache.service.GlobalCacheService;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.DoubleUtil;
import com.hymjweb.demo.framework.util.util.PoiUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_upload_file.bean.XtUploadFilePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_upload_file.service.XtUploadFileService;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.auto.bean.BCustomerAutoFreightAgreementPojo;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.auto.mapper.BCustomerAutoFreightAgreementMapper;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.bean.AgrementRatePojo;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.detail.bean.BCustomerFeightAgreementDetailPojo;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.detail.service.BCustomerFeightAgreementDetailService;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.ticket_price.bean.BCustomerFreightTicketPricePojo;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.ticket_price.service.BCustomerFreightTicketPriceService;
import com.hymjweb.demo.hymj.log_manage.aop.annotation.OperationLogger;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.util.*;

@Service
public class BCustomerAutoFreightAgreementService extends BaseService {

    @Autowired
    private BCustomerAutoFreightAgreementMapper bCustomerAutoFreightAgreementMapper;

    @Autowired
    private BCustomerFeightAgreementDetailService bCustomerFeightAgreementDetailService;

    @Autowired
    private BCustomerFreightTicketPriceService bCustomerFreightTicketPriceService;

    @Autowired
    private XtUploadFileService xtUploadFileService;

    @Autowired
    private GlobalCacheService globalCacheService;

    public List<BCustomerAutoFreightAgreementPojo> getAllList(Map param){
        return  bCustomerAutoFreightAgreementMapper.findAll(param);
    }

    public Object getPageList(Map param) {
        // 设置公共查询参数,加上部门
        this.setQueryCommParam(param);
        this.pageListInit(param);
        List list = getAllList(param);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }


    public BCustomerAutoFreightAgreementPojo convertToJavaBean(JSONObject agreementPojo) {

        String opFlag = DataTypeUtil.toStringObject(agreementPojo.get("opFlag"));

        BCustomerAutoFreightAgreementPojo pojo = agreementPojo.toJavaObject(BCustomerAutoFreightAgreementPojo.class);

        Date currentTime = this.getSysTime();
        String czyId = this.getLoginCzyId();

        if(opFlag.equals(SysConstant.OPER_ADD)){
            pojo.setId(this.getSysStrId());
            pojo.setCreateTime(currentTime);
            pojo.setCreateUserId(czyId);
        }

        pojo.setUpdateTime(currentTime);
        pojo.setUpdateUserId(czyId);

        for(BCustomerFeightAgreementDetailPojo detailPojo:pojo.getDetailPojos()){
            detailPojo.setId(this.getSysStrId());
            detailPojo.setAgreementFreightId(pojo.getId());
        }

        return pojo;

    }


    public int addAutoFreightAgreement(BCustomerAutoFreightAgreementPojo autoAgreementPojo) throws Exception {
        // 2.根据 customerId cityId 组成UK,检索是否有重复
        BCustomerAutoFreightAgreementPojo autoAgreementPojo1 = getAutoFreightAgreementPojo(autoAgreementPojo);
        if(autoAgreementPojo1 == null){
            // 可以添加
            int autoFreightAgreementPojoEffectRow = SpringAopUtil.getBean(this.getClass()).insert(autoAgreementPojo);
            return autoFreightAgreementPojoEffectRow;

        }
        return SysConstant.EXIST_STATUS.EXIST.getId();
    }

    public BCustomerAutoFreightAgreementPojo getAutoFreightAgreementPojo(BCustomerAutoFreightAgreementPojo autoAgreementPojo) {
        return bCustomerAutoFreightAgreementMapper.getAutoFreightAgreementPojo(autoAgreementPojo);
    }

    @OperationLogger(oper = SysConstant.OPER_ADD, operTitle="汽运协议运价-添加" , operMemo = "", classType =  BCustomerAutoFreightAgreementPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int insert(BCustomerAutoFreightAgreementPojo autoAgreementPojo) throws Exception {
        if(autoAgreementPojo==null){
            throwException("汽运协议运价添加参数错误!");
        }

        int effectRow = bCustomerAutoFreightAgreementMapper.insert(autoAgreementPojo);

        if(effectRow>SysConstant.INT_ZERO){
            int detailEffectRow = bCustomerFeightAgreementDetailService.batchInsert(autoAgreementPojo.getDetailPojos());
            if(detailEffectRow>SysConstant.INT_ZERO){
                return effectRow;
            }
            return SysConstant.INT_ZERO;
        }

        return SysConstant.INT_ZERO;
    }


    @Transactional(rollbackFor = {Exception.class})
    public int updateAutoFreightAgreement(BCustomerAutoFreightAgreementPojo autoAgreementPojo) throws Exception {
        if(autoAgreementPojo==null){
            throwException("汽运协议运价更新参数错误!");
        }
        // 直接更新
        int effectRow = SpringAopUtil.getBean(this.getClass()).update(autoAgreementPojo);

        if(effectRow>=SysConstant.INT_ZERO){
            // 删除原来的detail列表，然后再重新批量插入
            int deleteEffectRow = bCustomerFeightAgreementDetailService.deleteByAgreementFreightId(autoAgreementPojo.getId());
            if(deleteEffectRow>=SysConstant.INT_ZERO){
                int detailEffectRow = bCustomerFeightAgreementDetailService.batchInsert(autoAgreementPojo.getDetailPojos());
                if(detailEffectRow>SysConstant.INT_ZERO){
                    return effectRow;
                }
                else{
                    return SysConstant.INT_ZERO;
                }
            }
            else{
                return SysConstant.INT_ZERO;
            }
        }
        return SysConstant.INT_ZERO;
    }

    @OperationLogger(oper = SysConstant.OPER_UPDATE, operTitle="汽运协议运价-编辑" , operMemo = "", classType =  BCustomerAutoFreightAgreementPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int update(BCustomerAutoFreightAgreementPojo autoAgreementPojo) throws Exception {
        if(autoAgreementPojo==null){
            throwException("汽运协议运价更新参数错误!");
        }
        int effectRow = bCustomerAutoFreightAgreementMapper.update(autoAgreementPojo);
        return effectRow;
    }


    @OperationLogger(oper = SysConstant.OPER_DELETE, operTitle="汽运协议运价-删除" , operMemo = "", classType =  BCustomerAutoFreightAgreementPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int delete(String agreementFreightIds) throws Exception {
        if(DataTypeUtil.isEmptyStr(agreementFreightIds)){
            throwException("汽运协议运价删除参数错误!");
        }
        // 1.批量获取客户运价主表对象
        List<BCustomerAutoFreightAgreementPojo> autoPojos = bCustomerAutoFreightAgreementMapper.getAutoFreightAgreementPojoByIds(agreementFreightIds);
        // 2.遍历一下，获取所有文件对象IDS
        List<String> fileIds = new ArrayList<>();
        for(BCustomerAutoFreightAgreementPojo pojo: autoPojos){
            if(pojo.getFileId()!=null){
                fileIds.add(pojo.getFileId());
            }
        }

        // 3.批量删除detail表中的数据
        int detailDeleteEffectRows = bCustomerFeightAgreementDetailService.batchDeleteByAgreementFreightIds(agreementFreightIds);

        if(detailDeleteEffectRows>=SysConstant.INT_ZERO){
            // 4.删除主表对象对应的文件
            if(fileIds.size()>0){
                xtUploadFileService.deleteFile(fileIds);
            }

            // 5.删除主表对象
            int effectRows = bCustomerAutoFreightAgreementMapper.deleteByIds(agreementFreightIds);
            if(effectRows>=SysConstant.INT_ZERO){
                return effectRows;
            }
        }

        return SysConstant.INT_ZERO;
    }


    public int upload(MultipartFile upLoadFile) throws Exception {

        XtUploadFilePojo filePojo = xtUploadFileService.upload(upLoadFile);

        // 上传完成后进行数据导入工作
        int flag = excelToDatabase(filePojo);

        return flag;
    }

    private int excelToDatabase(XtUploadFilePojo filePojo) throws Exception {
        File destFile = xtUploadFileService.getFile(filePojo);

        // 定义读取文件的输入流
        FileInputStream fis = new FileInputStream(destFile);;

        Workbook wb = PoiUtil.getWorkBook(destFile, fis);

        Sheet sheet = wb.getSheetAt(0);

        int rows = sheet.getLastRowNum();

        if (rows < 2) {
            throwException("Excel文件内容为空");
        }

        // 1.按行读取所有Excel中的每一行，形成一个List<Map>
        List<Map<String,Object>> listMap = new ArrayList<>();

        // 异常处理字符串
        StringBuffer exceptionInfoStr = new StringBuffer();

        // 检测是否有UK重复
        Set<String> airCheckObjSet = new HashSet<>();

        for (int i = 1; i <= rows; i++){
            Map<String,Object> map = new HashMap<>();
            Row row = sheet.getRow(i);
            int rowNum = row.getRowNum() + 1;
            if (row != null){
                // 序号(0) 客户(1)	目的站(2)	运价标准类型(3)	货物类型(4)	起始值(5)	结束值(6)	单价(7)	范围说明(8)	备注(9)
                // 客户名称
                String customerName = PoiUtil.getExcelCellValue(wb, row, 1);
                if(DataTypeUtil.isEmptyStr(customerName)){
                    exceptionInfoStr.append("上传的excel文件中的【" + rowNum + "】行【客户】为空<br/>");
                }

                map.put("customerName",customerName);

                // 目的站
                String cityName = PoiUtil.getExcelCellValue(wb, row, 2);
                if (DataTypeUtil.isEmptyStr(cityName)) {
                    exceptionInfoStr.append("上传的excel文件中的【" + rowNum + "】行【目的站】为空<br/>");
                }
                map.put("cityName",cityName);


                //客户协议运价标准分类standardTypeId
                String standardTypeName = PoiUtil.getExcelCellValue(wb, row, 3);
                if (DataTypeUtil.isEmptyStr(standardTypeName)) {
                    exceptionInfoStr.append("上传的excel文件中的【" + rowNum + "】行【运价标准类型】为空<br/>");
                }
                map.put("standardTypeName",standardTypeName);

                //货物类型goodsTypeId
                String goodsTypeName = PoiUtil.getExcelCellValue(wb, row, 4);
                if (DataTypeUtil.isEmptyStr(goodsTypeName)) {
                    exceptionInfoStr.append("上传的excel文件中的【" + rowNum + "】行【货物类型】为空<br/>");
                }
                map.put("goodsTypeName",goodsTypeName);

                //起始值
                Integer startValue = DataTypeUtil.toInteger(PoiUtil.getExcelCellValue(wb, row, 5));
                if (DataTypeUtil.isEmptyStr(startValue)) {
                    exceptionInfoStr.append("上传的excel文件中的【" + rowNum + "】行【起始值】为空<br/>");
                }
                map.put("startValue",startValue);

                //结束值
                Integer endValue = DataTypeUtil.toInteger(PoiUtil.getExcelCellValue(wb, row, 6));
                if (DataTypeUtil.isEmptyStr(endValue)) {
                    exceptionInfoStr.append("上传的excel文件中的【" + rowNum + "】行【结束值】为空<br/>");
                }
                map.put("endValue",endValue);

                //单价
                Double price = DataTypeUtil.toDouble(PoiUtil.getExcelCellValue(wb, row, 7));
                if (DataTypeUtil.isEmptyStr(price)) {
                    exceptionInfoStr.append("上传的excel文件中的【" + rowNum + "】行【单价】为空<br/>");
                }
                map.put("price",price);

                String standardComment = PoiUtil.getExcelCellValue(wb, row, 8);
                String comment = PoiUtil.getExcelCellValue(wb, row, 9);

                map.put("standardComment",standardComment);
                map.put("comment",comment);

                airCheckObjSet.add(customerName + cityName);

                listMap.add(map);
            }
        }

        if(exceptionInfoStr.length()>0){
            deleteFile(filePojo);
            throwException(exceptionInfoStr.toString());
        }

        // 2.校验所有List中的Map的相应字段值是否一样,构成的一个Key
        // 客户	始发机场	目的站	航空公司	航班号
        if(airCheckObjSet.size()>1){
            deleteFile(filePojo);
            throwException("导入的数据【客户】【目的站】【起票价】列有不同的值，不能导入！");
        }


        Map<String,Object> mapObj = listMap.get(0);
        // 客户customerName
        String customerId = globalCacheService.getIdByCacheNameAndName(SysConstant.CACHE_M_CUSTOMER,mapObj.get("customerName").toString());
        if(customerId == null){
            exceptionInfoStr.append("上传的excel文件中的【客户:" + mapObj.get("customerName").toString() + "】 在客户列表中找不到对应ID<br/>");
        }

        //目的站
        String cityId = globalCacheService.getIdByCacheNameAndName(SysConstant.CACHE_XT_CITY,mapObj.get("cityName").toString());
        if(cityId == null){
            exceptionInfoStr.append("上传的excel文件中的【目的站:" + mapObj.get("cityName").toString() + "】在系统区域表中找不到对应ID<br/>");
        }


        if(exceptionInfoStr.length()>0){
            deleteFile(filePojo);
            throwException(exceptionInfoStr.toString());
        }

        Date currentTime = this.getSysTime();
        String czyId = this.getLoginCzyId();
        // 生成主表对象
        BCustomerAutoFreightAgreementPojo autoPojo = new BCustomerAutoFreightAgreementPojo();
        autoPojo.setCustomerId(customerId);
        autoPojo.setCityId(cityId);
        autoPojo.setFileId(filePojo.getId());// 要写入文件数据库
        autoPojo.setComment(mapObj.get("comment").toString());
        autoPojo.setCreateTime(currentTime);
        autoPojo.setCreateUserId(czyId);
        autoPojo.setUpdateTime(currentTime);
        autoPojo.setUpdateUserId(czyId);
        autoPojo.setDetailPojos(new ArrayList<>());

        String opType = SysConstant.OPER_ADD; // 这个代表插入
        // 检测airPojo 否存在,如果存在就删除其明细，同时列新airPojo对象，注意这个地方可能要更换ID,
        BCustomerAutoFreightAgreementPojo newAutoPojo = getAutoFreightAgreementPojo(autoPojo);
        if(newAutoPojo != null){
            autoPojo.setId(newAutoPojo.getId());
            opType = SysConstant.OPER_UPDATE;
        }
        else{
            autoPojo.setId(this.getSysStrId());
        }


        // 定义四个detail对象集合
        List<BCustomerFeightAgreementDetailPojo> weightGeneralDetailList = new ArrayList<>();// 重量 普货
        List<BCustomerFeightAgreementDetailPojo> weightFreshDetailList = new ArrayList<>();  // 重量 鲜活
        List<BCustomerFeightAgreementDetailPojo> volumeGeneralDetailList = new ArrayList<>(); // 体积 普货
        List<BCustomerFeightAgreementDetailPojo> volumeFreshDetailList = new ArrayList<>();  // 体积 鲜活

        // 迭代生成detail对象
        for(int i=0;i<listMap.size();i++){
            Map<String,Object> objMap = listMap.get(i);
            String standardTypeName = objMap.get("standardTypeName").toString();
            String goodsTypeName = objMap.get("goodsTypeName").toString();
            //客户协议运价标准分类standardTypeId
            Long standardTypeId = DataTypeUtil.toLongObject(globalCacheService.getIdByCacheNameAndName(SysConstant.CACHE_XT_DIC,standardTypeName));
            if(standardTypeId == null){
                exceptionInfoStr.append("上传的excel文件中的【运价标准类型:" + standardTypeName + "】在字典表中找不到对应ID<br/>");
            }

            //货物类型goodsTypeId
            Long goodsTypeId = DataTypeUtil.toLongObject(globalCacheService.getIdByCacheNameAndName(SysConstant.CACHE_XT_DIC,goodsTypeName));
            if(goodsTypeId == null){
                exceptionInfoStr.append("上传的excel文件中的【货物类型:" + goodsTypeName + "】在字典表中找不到对应ID<br/>");
            }

            // 起始值	结束值	单价	范围说明
            Integer startValue = DataTypeUtil.toInteger(objMap.get("startValue"));
            Integer endValue = DataTypeUtil.toInteger(objMap.get("endValue"));
            Double price = DataTypeUtil.toDouble(objMap.get("price"));
            String comment = DataTypeUtil.toStringObject(objMap.get("standardComment"));

            if(DataTypeUtil.isEmptyStr(startValue) || startValue<0){
                exceptionInfoStr.append("上传的excel文件中的【起始值】列有非整数值或值小于零<br/>");
            }
            if(DataTypeUtil.isEmptyStr(endValue) || startValue<0){
                exceptionInfoStr.append("上传的excel文件中的【结束值】列有非整数值或值小于零<br/>");
            }
            if(DataTypeUtil.isEmptyStr(price) || startValue<0){
                exceptionInfoStr.append("上传的excel文件中的【单价】列有非数值或值小于零<br/>");
            }

            // 可以生成对象
            BCustomerFeightAgreementDetailPojo detailPojo = new BCustomerFeightAgreementDetailPojo();
            detailPojo.setId(this.getSysStrId());
            detailPojo.setAgreementFreightTypeId(SysConstant.XT_DIC_TRANSPORT_SPECIES.AUTO.getId());
            detailPojo.setAgreementFreightId(autoPojo.getId());
            detailPojo.setStandardTypeId(standardTypeId);
            detailPojo.setGoodsTypeId(goodsTypeId);
            detailPojo.setStartValue(startValue);
            detailPojo.setEndValue(endValue);
            detailPojo.setPrice(price);
            detailPojo.setComment(comment);

            if(standardTypeId.longValue() == SysConstant.XT_DIC_CUSTOMER_AGREEMENT_FREIGHT_STANDARD.weight.getId()){
                if(goodsTypeId.longValue() == SysConstant.XT_DIC_GOODS_TYPE.GENERAL_CARGO.getId()){
                    weightGeneralDetailList.add(detailPojo);
                }
                else{
                    weightFreshDetailList.add(detailPojo);
                }
            }
            else{
                if(goodsTypeId.longValue() == SysConstant.XT_DIC_GOODS_TYPE.GENERAL_CARGO.getId()){
                    volumeGeneralDetailList.add(detailPojo);
                }
                else{
                    volumeFreshDetailList.add(detailPojo);
                }
            }
        }

        if(exceptionInfoStr.length()>0){
            deleteFile(filePojo);
            throwException(exceptionInfoStr.toString());
        }

        // 下面开始排序
        if(weightGeneralDetailList.size()>1){

            //Collections.sort(weightGeneralDetailList);
            boolean result = bCustomerFeightAgreementDetailService.checkSquence(weightFreshDetailList);
            if(!result){
                deleteFile(filePojo);
                throwException("【重量运价标准】【普货】运价有非连续值存在，请修改!");
            }
        }
        if(weightFreshDetailList.size()>1){
            //Collections.sort(weightFreshDetailList);
            boolean result = bCustomerFeightAgreementDetailService.checkSquence(weightFreshDetailList);
            if(!result){
                deleteFile(filePojo);
                throwException("【重量运价标准】【鲜活】运价有非连续值存在，请修改!");
            }
        }
        if(volumeGeneralDetailList.size()>1){
            //Collections.sort(volumeGeneralDetailList);
            boolean result = bCustomerFeightAgreementDetailService.checkSquence(volumeGeneralDetailList);
            if(!result){
                deleteFile(filePojo);
                throwException("【体积运价标准】【普货】运价有非连续值存在，请修改!");
            }
        }
        if(volumeFreshDetailList.size()>1){
            //Collections.sort(volumeFreshDetailList);
            boolean result = bCustomerFeightAgreementDetailService.checkSquence(volumeFreshDetailList);
            if(!result){
                deleteFile(filePojo);
                throwException("【体积运价标准】【鲜活】运价有非连续值存在，请修改!");
            }
        }

        // 到此全部校验完成，生成统一的集合放到pojo中
        autoPojo.getDetailPojos().addAll(weightGeneralDetailList);
        autoPojo.getDetailPojos().addAll(weightFreshDetailList);
        autoPojo.getDetailPojos().addAll(volumeGeneralDetailList);
        autoPojo.getDetailPojos().addAll(volumeFreshDetailList);

        // 正式入库
        return saveExcelObjToDb(autoPojo,opType);
    }



    private void deleteFile(XtUploadFilePojo filePojo){
        xtUploadFileService.deleteFile(filePojo);
    }


    @Transactional(rollbackFor = {Exception.class})
    public int saveExcelObjToDb(BCustomerAutoFreightAgreementPojo autoPojo,String opType) throws Exception {

        int effectRow = 0;

        if(opType.equals(SysConstant.OPER_ADD)){
            effectRow = insert(autoPojo);
        }
        else{
            effectRow = updateAutoFreightAgreement(autoPojo);
        }

        return effectRow;
    }


    public AgrementRatePojo loadAirAgrementRatePojo(Map param) {
        AgrementRatePojo agrementRatePojo = new AgrementRatePojo();
        agrementRatePojo.setWeightRate(DoubleUtil.round2(0D));
        agrementRatePojo.setVolumeRate(DoubleUtil.round2(0D));
        agrementRatePojo.setWeightStandardTicketPrice(DoubleUtil.round2(0D));
        agrementRatePojo.setVolumeStandardTicketPrice(DoubleUtil.round2(0D));

        //{customerId:deliveryCustomerIdValue,
        // cityId:destCityIdValue,
        // goodsTypeId:goodsTypeIdValue,
        // chargeableWeight:chargeableWeightValue,
        // volume:volumeValue}

        String customerId = DataTypeUtil.toStringObject(param.get("customerId"));
        String cityId = DataTypeUtil.toStringObject(param.get("cityId"));
        Long goodsTypeId = DataTypeUtil.toLongObject(param.get("goodsTypeId"));
        Double chargeableWeight = DataTypeUtil.toDouble(param.get("chargeableWeight"));
        Double volume = DataTypeUtil.toDouble(param.get("volume"));

        // 1.根据 customerId airportId cityId airlinesId  flightNo 锁定 b_customer_auto_freight_agreement记录
        BCustomerAutoFreightAgreementPojo newPojo = new BCustomerAutoFreightAgreementPojo();
        newPojo.setCustomerId(customerId);
        newPojo.setCityId(cityId);

        // 这才是真正从数据中获取的对象
        BCustomerAutoFreightAgreementPojo pojo = bCustomerAutoFreightAgreementMapper.getAutoFreightAgreementPojo(newPojo);

        if(pojo != null){
            // 根据 id 获取对应的列表对象
            List<BCustomerFeightAgreementDetailPojo> detailPojos = bCustomerFeightAgreementDetailService.getDetailListByAgreementFreightId(pojo.getId());
            // 1.分离出重量与体积标准两个集合
            List<BCustomerFeightAgreementDetailPojo> weightList = new ArrayList<>();
            List<BCustomerFeightAgreementDetailPojo> volumeList = new ArrayList<>();

            for(BCustomerFeightAgreementDetailPojo detailPojo : detailPojos){
                Long detailPojoStandardTypeId = detailPojo.getStandardTypeId();
                Long detailPojoGoodsTypeId = detailPojo.getGoodsTypeId();
                if(detailPojoStandardTypeId.longValue() == SysConstant.XT_DIC_CUSTOMER_AGREEMENT_FREIGHT_STANDARD.weight.getId()){
                    if(detailPojoGoodsTypeId.longValue() == goodsTypeId.longValue()){
                        weightList.add(detailPojo);
                    }
                }
                else{
                    if(detailPojoGoodsTypeId.longValue() == goodsTypeId.longValue()){
                        volumeList.add(detailPojo);
                    }
                }
            }

            // 2.排序一下
            if(weightList.size()>1){
                //Collections.sort(weightList);
            }

            if(volumeList.size()>1){
                //Collections.sort(volumeList);
            }

            // 3.开始按值进行寻找
            for(int i=0;i<weightList.size();i++){
                BCustomerFeightAgreementDetailPojo weightDetailPojo = weightList.get(i);
                Integer startValue = weightDetailPojo.getStartValue();
                Integer endValue = weightDetailPojo.getEndValue();
                if(chargeableWeight.doubleValue()>=startValue.intValue() && chargeableWeight.doubleValue()<endValue.intValue()){
                    agrementRatePojo.setWeightRate(DoubleUtil.round2(weightDetailPojo.getPrice()));
                    break;
                }
            }

            for(int i=0;i<volumeList.size();i++){
                BCustomerFeightAgreementDetailPojo volumeDetailPojo = volumeList.get(i);
                Integer startValue = volumeDetailPojo.getStartValue();
                Integer endValue = volumeDetailPojo.getEndValue();
                if(volume.doubleValue()>=startValue.intValue() && volume.doubleValue()<endValue.intValue()){
                    agrementRatePojo.setVolumeRate(DoubleUtil.round2(volumeDetailPojo.getPrice()));
                    break;
                }
            }
        }


        Map<String,Object> ticketPriceQueryMap = new HashMap<>();
        ticketPriceQueryMap.put("freightAgreementId",pojo.getId());
        ticketPriceQueryMap.put("goodsTypeId",goodsTypeId);

        List<BCustomerFreightTicketPricePojo> ticketPricePojoList = bCustomerFreightTicketPriceService.queryNoPageList(ticketPriceQueryMap);

        // 遍历集合，根据类型来设置参数
        for(BCustomerFreightTicketPricePojo ticketPricePojo : ticketPricePojoList){
            if(ticketPricePojo.getStandardTypeId().longValue() == SysConstant.XT_DIC_CUSTOMER_AGREEMENT_FREIGHT_STANDARD.weight.getId()
                    && ticketPricePojo.getStandardTypeId().longValue() == SysConstant.XT_DIC_GOODS_TYPE.GENERAL_CARGO.getId()){
                agrementRatePojo.setWeightStandardTicketPrice(ticketPricePojo.getTicketPrice());
            }
            else{
                agrementRatePojo.setVolumeStandardTicketPrice(ticketPricePojo.getTicketPrice());
            }
        }

        return agrementRatePojo;
    }

    public BCustomerAutoFreightAgreementPojo getPojoByUk(Map param) {
        /**
         * let airWeightUrl = baseUrl + "standardTypeId=" + standardType.weight;
         *             airWeightUrl += "&customerId=" + deliveryCustomerIdValue;
         *             airWeightUrl += "&cityId=" + destCityIdValue;
         */

        String customerId = DataTypeUtil.toStringObject(param.get("customerId"));
        String cityId = DataTypeUtil.toStringObject(param.get("cityId"));

        BCustomerAutoFreightAgreementPojo newPojo = new BCustomerAutoFreightAgreementPojo();
        newPojo.setCustomerId(customerId);
        newPojo.setCityId(cityId);

        // 这才是真正从数据中获取的对象
        BCustomerAutoFreightAgreementPojo pojo = bCustomerAutoFreightAgreementMapper.getAutoFreightAgreementPojo(newPojo);
        return pojo;
    }
}
